package org.jmx;

import com.sun.jdmk.comm.HtmlAdaptorServer;

import javax.management.*;
import java.lang.management.ManagementFactory;

/**
* @author James
* @date 2018年8月15日 
*/
public class HelloWorldAgent implements NotificationListener{

	private MBeanServer mbs;
	
	public HelloWorldAgent() {
		this.mbs=MBeanServerFactory.createMBeanServer("HelloWorldAgent");
		HelloWorld hw=new HelloWorld();
		ObjectName helloWorldName=null;
		try {
			helloWorldName=new ObjectName("HelloAgent");
			mbs.registerMBean(hw, helloWorldName);
		} catch (MalformedObjectNameException e) {
			e.printStackTrace();
		} catch (InstanceAlreadyExistsException e) {
			e.printStackTrace();
		} catch (MBeanRegistrationException e) {
			e.printStackTrace();
		} catch (NotCompliantMBeanException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) throws Exception {
//		MBeanServer server = ManagementFactory.getPlatformMBeanServer();
//		ObjectName helloName = new ObjectName("jmxBean:name=hello");
//		//create mbean and register mbean
//		server.registerMBean(new HelloWorld(), helloName);
//		Thread.sleep(60*60*1000);
		
		MBeanServer server = ManagementFactory.getPlatformMBeanServer();
        ObjectName helloName = new ObjectName("jmxBean:name=hello");
        //create mbean and register mbean
        server.registerMBean(new HelloWorld(), helloName);
        
        ObjectName adapterName = new ObjectName("HelloAgent:name=htmladapter,port=8082");   
        HtmlAdaptorServer adapter = new HtmlAdaptorServer();   
        server.registerMBean(adapter, adapterName);  
        adapter.start();
	}
	
	@Override
	public void handleNotification(Notification notification, Object handback) {
		System.out.println("handleNotification() work");
	}

}
